home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / CONNECT.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  18KB  |  539 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is Winsock Chess specific.
  12.   
  13.   C source for GNU CHESS
  14.  
  15.   Revision: 1990-09-30
  16.   Modified by Daryl Baker for use in MS WINDOWS environment
  17.  
  18.   Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  19.   Copyright (c) 1988, 1989, 1990  John Stanback
  20.  
  21.   This file is part of CHESS.
  22.  
  23.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  24.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  25.   the consequences of using it or for whether it serves any particular
  26.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  27.   General Public License for full details.
  28.  
  29.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  30.   only under the conditions described in the CHESS General Public License.
  31.   A copy of this license is supposed to have been given to you along with
  32.   CHESS so you can know your rights and responsibilities.  It should be in a
  33.   file named COPYING.  Among other things, the copyright notice and this
  34.   notice must be preserved on all copies.
  35. */
  36.  
  37. #define NOATOM              
  38. #define NOCREATESTRUCT
  39. #define NOFONT
  40. #define NOREGION
  41. #define NOSOUND
  42. #define NOWH
  43. #define NOKANJI
  44.  
  45. #define STRICT 
  46. #include <windows.h>         
  47. #include <windowsx.h> 
  48. #include <ddeml.h>
  49.  
  50. #include "winsock.h"
  51. #include "gnuchess.h"
  52. #include "defs.h"    
  53. #include "chess.h"
  54. #include "resource.h"
  55.  
  56. extern WORD wComType;
  57. extern BOOL (*Initialise)(void);
  58. extern BOOL (*ConnectHost)(void);
  59. extern BOOL (*ConnectClient)(void);
  60. extern void (*GetOpponentsMove)(WORD);
  61. extern void (*SendMove)(WORD);         
  62. extern BOOL (*IsCommand)(LPSTR);
  63. extern void (*LoadGame)(void);
  64. extern void (*StartGame)(void);
  65. extern void (*SetTime)(int);
  66. extern MoveInfo moveinfo;
  67. extern BOOL bConnected,bHost,bWaiting;
  68. extern HWND hwndHostDlg,hwndMain;               
  69. extern DWORD idDdeServInst,idDdeClntInst;
  70. extern HCONV hconvHost,hconvClient;
  71. extern HSZ hszServName,hszTopic,hszServNameCl,hszTopicCl,hszGet;
  72. extern int User_Move;    
  73. extern char szSockDesc[45];
  74. extern SOCKET socketListen,socketServer,socketClient;
  75. extern HINSTANCE hInst; 
  76.  
  77. void DestroySocket(SOCKET);
  78. void DecodeGame (HWND,LPSTR);
  79. void WaitForSocket(void);
  80. void SaveHosts(HWND, char *);
  81. void GetHosts(HWND);
  82. int BlockReceive(SOCKET,LPSTR,WORD);
  83. int BlockSend(SOCKET ,LPSTR, WORD);
  84. void EnableMenuItems(void);
  85.  
  86. BOOL CALLBACK ProtocolDlg(HWND hDlg, UINT message, WPARAM wParam, 
  87.                           LPARAM lParam)
  88. //---------------------------------------------------------------                          
  89. { WORD wOldComType;
  90.  
  91.   switch (message) 
  92.    { case WM_INITDIALOG:      
  93.        wOldComType = wComType;
  94.        CheckRadioButton(hDlg,IDC_SOCKETS,IDC_MODEM,wComType);
  95.        return TRUE;
  96.  
  97.      case WM_COMMAND:     
  98.         switch (wParam)
  99.          { case IDOK : 
  100.               EndDialog(hDlg, NULL);
  101.               return TRUE;
  102.            
  103.            case IDCANCEL :
  104.               wComType = wOldComType;
  105.               EndDialog(hDlg, NULL);
  106.               return TRUE;
  107.                  
  108.            case IDC_SOCKETS : 
  109.               GetOpponentsMove = GetOpponentsMoveWS;
  110.               SendMove         = SendMoveWS;
  111.               LoadGame         = GetGameWS;
  112.               IsCommand        = IsCommandWS;
  113.               StartGame        = NewGameWS;
  114.               SetTime          = SetTimeWS;
  115.               wComType = IDC_SOCKETS;
  116.               return TRUE;
  117.               
  118.            case IDC_DDE :
  119.               GetOpponentsMove = GetOpponentsMoveDDE;
  120.               SendMove         = SendMoveDDE;
  121.               LoadGame         = GetGameDDE;
  122.               IsCommand        = IsCommandDDE;
  123.               StartGame        = NewGameDDE;
  124.               SetTime          = SetTimeDDE;
  125.               wComType = IDC_DDE;
  126.               return TRUE;
  127.               
  128.            case IDC_MODEM :
  129.               wComType = IDC_MODEM;
  130.               return TRUE;                 
  131.          }
  132.     }
  133.  
  134.     return (FALSE); 
  135. }
  136.  
  137. BOOL IsCommandDDE(LPSTR lpszCommand)
  138. //-----------------=----------------
  139. { HDDEDATA hddedata;
  140.   LPSTR lpszGetBuf;
  141.   DWORD dwBuflen;
  142.   char str[10];
  143.   short opp;
  144.  
  145.   if (lstrcmpi(lpszCommand,"COMG") == 0)
  146.     { hddedata = DdeClientTransaction(NULL,0,hconvClient,hszGet,CF_TEXT,
  147.                                   XTYP_REQUEST, 2000, NULL);
  148.       NewGame(hwndMain);                            
  149.       lpszGetBuf = (LPSTR)DdeAccessData(hddedata,&dwBuflen);
  150.       DecodeGame(hwndMain,lpszGetBuf);
  151.       flag.reverse = !flag.reverse;   
  152.       DdeUnaccessData(hddedata);
  153.       bWaiting=FALSE;
  154.       User_Move = FALSE;
  155.       UpdateDisplay (hwndMain, 0, 0, 1, 0);
  156.       return TRUE;
  157.     }       
  158.     
  159.   if (lstrcmpi(lpszCommand,"COMN") == 0)
  160.     { opp = opponent;
  161.       NewGame(hwndMain);
  162.       if (opp == black)
  163.         { computer = white;
  164.           opponent = black;
  165.           User_Move = TRUE;          
  166.         }
  167.       else
  168.         { computer = black;
  169.           opponent = white;
  170.           User_Move = FALSE;
  171.           flag.reverse = !flag.reverse;          
  172.         }  
  173.       UpdateDisplay(hwndMain,0,0,1,0);
  174.       flag.force = false;
  175.       Sdepth = 0; 
  176.       bWaiting = FALSE; 
  177.       return TRUE;
  178.     }  
  179.   if (lstrcmpi(lpszCommand,"COMT") == 0)
  180.     { sscanf(moveinfo.szMove,"%4s%3d%3d",str,&TCmoves,&TCminutes);
  181.       if (TCflag)
  182.          SetTimeControl ();
  183.       bWaiting=FALSE;
  184.       return TRUE;
  185.     }  
  186.   return FALSE;      
  187. }
  188.  
  189. BOOL CALLBACK LdRequestDlg(HWND hDlg, UINT message, WPARAM wParam,
  190.                            LPARAM lParam)
  191. //----------------------------------------------------------------
  192. { char *szPrompt;
  193.  
  194.   switch (message) 
  195.    { case WM_INITDIALOG : 
  196.        szPrompt = (char *) LOWORD((DWORD)lParam);
  197.        SetDlgItemText(hDlg,IDS_PROMPT,szPrompt);
  198.        return TRUE;
  199.    
  200.      case WM_COMMAND:     
  201.         switch (wParam)
  202.          { case IDB_AGREE : 
  203.               EndDialog(hDlg,TRUE);
  204.               return TRUE;   
  205.            case ID_NO :
  206.               EndDialog(hDlg,FALSE);
  207.               return TRUE;
  208.          }
  209.    }
  210.   return (FALSE); 
  211. }
  212.  
  213.  
  214. BOOL IsCommandWS(LPSTR lpszCommand)
  215. //-----------------=----------------
  216. { LPSTR lpszGetBuf;
  217.   SOCKET socket;
  218.   unsigned long ulArgp;
  219.   char szPrompt[80]; 
  220.   short opp;
  221.  
  222.   if (lstrcmpi(lpszCommand,"COMG") == 0)
  223.     { if (bHost)
  224.         socket = socketServer;
  225.       else
  226.         socket = socketClient; 
  227.       lstrcpy(szPrompt,"Opponent wants to load a saved game");  
  228.       if (DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_REQUEST),hwndMain,
  229.                     LdRequestDlg,MAKELPARAM(szPrompt,0)))
  230.         { WSAAsyncSelect(socket,hwndMain,0,0L);
  231.           ulArgp = 0;  
  232.           ioctlsocket(socket,FIONBIO,&ulArgp);
  233.           lpszGetBuf = (LPSTR)GlobalAllocPtr(GMEM_MOVEABLE | GMEM_ZEROINIT,
  234.                                              4096);
  235.           if (lpszGetBuf == NULL)
  236.             { MsgBox( NULL,
  237.                       MB_ICONSTOP | MB_OK,
  238.                      "Out of global memory");
  239.               BlockSend(socket,"NO",3);
  240.             }
  241.           else  
  242.             { BlockSend(socket,"OK",3); 
  243.               BlockReceive(socket,lpszGetBuf,4095); 
  244.               NewGame(hwndMain);                            
  245.               DecodeGame(hwndMain,lpszGetBuf);
  246.               flag.reverse = !flag.reverse;   
  247.               GlobalFreePtr(lpszGetBuf);
  248.             }  
  249.         } 
  250.       else
  251.         BlockSend(socket,"NO",3);
  252.       bWaiting=FALSE;
  253.       User_Move = FALSE;
  254.       Sdepth = 0;
  255.       UpdateDisplay (hwndMain, 0, 0, 1, 0);
  256.       ulArgp = 1;  
  257.       ioctlsocket(socket,FIONBIO,&ulArgp);
  258.       WSAAsyncSelect(socket,hwndMain,WM_SOCKET,
  259.                          FD_READ | FD_CLOSE);      
  260.       return TRUE;
  261.     }                      
  262.     
  263.   if (lstrcmpi(lpszCommand,"COMN") == 0)
  264.     { if (bHost)
  265.         socket = socketServer;
  266.       else
  267.         socket = socketClient; 
  268.       lstrcpy(szPrompt,"Opponent wants to start a new game");  
  269.       if (DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_REQUEST),hwndMain,
  270.                     LdRequestDlg,MAKELPARAM(szPrompt,0)))
  271.         { WaitForSocket();
  272.           WSAAsyncSelect(socket,hwndMain,0,0L);
  273.           ulArgp = 0;  
  274.           ioctlsocket(socket,FIONBIO,&ulArgp);          
  275.           BlockSend(socket,"OK",3); 
  276.           opp = opponent;
  277.           NewGame(hwndMain);
  278.           if (opp == black)
  279.            { computer = white;  // they get swapped on return ie reverse logic
  280.              opponent = black;
  281.              User_Move = TRUE;          
  282.            }
  283.           else
  284.            { computer = black;
  285.              opponent = white;
  286.              User_Move = FALSE;
  287.              flag.reverse = !flag.reverse;          
  288.            }  
  289.           UpdateDisplay(hwndMain,0,0,1,0);
  290.           flag.force = false;
  291.           Sdepth = 0; 
  292.         }  
  293.       else
  294.         BlockSend(socket,"NO",3);        
  295.       bWaiting=FALSE;    
  296.       ulArgp = 1;  
  297.       ioctlsocket(socket,FIONBIO,&ulArgp);
  298.       WSAAsyncSelect(socket,hwndMain,WM_SOCKET,
  299.                          FD_READ | FD_CLOSE);          
  300.       return TRUE;                   
  301.     }    
  302.     
  303.     if (lstrcmpi(lpszCommand,"COMT") == 0)
  304.     { if (bHost)
  305.         socket = socketServer;
  306.       else
  307.         socket = socketClient; 
  308.       lstrcpy(szPrompt,"Opponent wants to change time settings");  
  309.       if (DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_REQUEST),hwndMain,
  310.                     LdRequestDlg,MAKELPARAM(szPrompt,0)))
  311.         { WaitForSocket();
  312.           WSAAsyncSelect(socket,hwndMain,0,0L);
  313.           ulArgp = 0;  
  314.           ioctlsocket(socket,FIONBIO,&ulArgp);          
  315.           BlockSend(socket,"OK",3); 
  316.           BlockReceive(socket,szPrompt,7);
  317.           sscanf(szPrompt,"%3d%3d",&TCmoves,&TCminutes);
  318.           SetTimeControl ();
  319.         }  
  320.       else
  321.         BlockSend(socket,"NO",3);        
  322.       bWaiting=FALSE;  
  323.       ulArgp = 1;  
  324.       ioctlsocket(socket,FIONBIO,&ulArgp);
  325.       WSAAsyncSelect(socket,hwndMain,WM_SOCKET,
  326.                          FD_READ | FD_CLOSE);          
  327.       return TRUE;
  328.     }  
  329.   
  330.   return FALSE;      
  331. }     
  332.  
  333. BOOL CALLBACK HostDlgDDE(HWND hDlg, UINT message, WPARAM wParam, 
  334.                           LPARAM lParam)
  335. //-----------------------------------------------------------
  336. { HSZ hszServer;
  337.  
  338.   switch (message) 
  339.    { case WM_INITDIALOG:       
  340.        hwndHostDlg = hDlg;
  341.        bHost = TRUE;  
  342.        if (!InitialiseDDE())
  343.          { MessageBox(hDlg,"Error initializing DDE Link",
  344.                       (LPSTR)NULL, MB_OK | MB_ICONSTOP);
  345.            hwndHostDlg = NULL;           
  346.            bHost = FALSE;
  347.            EndDialog(hDlg, NULL);
  348.            return TRUE;
  349.          }       
  350.        SetWindowText(hwndMain,"Winsock Chess (Waiting for Connection)");  
  351.        return TRUE;
  352.  
  353.      case WM_CONNECTED :       
  354.        hwndHostDlg = NULL;
  355.        hszServer = DdeCreateStringHandle(idDdeClntInst,"WSCHESSC",NULL);
  356.        hconvClient = DdeConnect(idDdeClntInst,hszServer,hszTopicCl,NULL);
  357.        if (hconvClient == NULL)
  358.          MessageBox(hDlg,"Could not open duplex link on DDE connect",
  359.                     (LPSTR)NULL, MB_OK | MB_ICONSTOP);
  360.        else
  361.          bConnected = TRUE;
  362.        EnableMenuItems();  
  363.        SetWindowText(hwndMain,"Winsock Chess (Connected)");    
  364.        EndDialog(hDlg, NULL);       
  365.        return TRUE;   
  366.  
  367.      case WM_COMMAND:     
  368.         switch (wParam)
  369.          { case IDC_ABORT : 
  370.               hwndHostDlg = NULL; 
  371.               bHost = FALSE; 
  372.               EndDialog(hDlg, NULL);
  373.               return TRUE;   
  374.               
  375.          }
  376.     }
  377.  
  378.     return (FALSE); 
  379. }
  380.  
  381. BOOL CALLBACK ConnDdeDlg(HWND hDlg, UINT message, WPARAM wParam, 
  382.                           LPARAM lParam)
  383. //--------------------------------------------------------------
  384. { HSZ hszServer;
  385.  
  386.   switch (message) 
  387.    { case WM_INITDIALOG:      
  388.        if (!InitialiseDDE())
  389.          { MessageBox(hDlg,"Error initializing DDEML",
  390.                       (LPSTR)NULL, MB_OK | MB_ICONSTOP);
  391.            EndDialog(hDlg, NULL);
  392.            return TRUE;
  393.          }
  394.        hszServer   = DdeCreateStringHandle(idDdeClntInst,"WSCHESSH",NULL);
  395.        hconvClient = DdeConnect(idDdeClntInst,hszServer,hszTopicCl,NULL);
  396.        if (hconvClient == NULL)
  397.          { SetWindowText(GetDlgItem(hDlg,IDC_CMESS),"Connection refused");
  398.            SetWindowText(hwndMain,"Winsock Chess (Not Connected)");
  399.          }  
  400.        else
  401.          { SetWindowText(GetDlgItem(hDlg,IDC_CMESS),"Connected");
  402.            SetWindowText(hwndMain,"Winsock Chess (Connected)");  
  403.          }  
  404.        return TRUE;
  405.  
  406.      case WM_COMMAND:     
  407.         switch (wParam)
  408.          { case IDOK : 
  409.              if (hconvClient != NULL)
  410.                { computer = white;
  411.                  opponent = black;
  412.                  User_Move = FALSE;
  413.                  bConnected = TRUE;
  414.                  flag.reverse = !flag.reverse;
  415.                  UpdateDisplay(hwndMain,0,0,1,0);
  416.                  flag.force = false;
  417.                  Sdepth = 0;
  418.                  PostMessage ( hwndMain, MSG_COMPUTER_MOVE, NULL,NULL);  
  419.                }
  420.              else
  421.                bConnected = FALSE;    
  422.              EnableMenuItems();  
  423.              EndDialog(hDlg, NULL);
  424.              return TRUE;
  425.          }     
  426.     }
  427.  
  428.     return (FALSE); 
  429. }
  430.  
  431. BOOL CALLBACK HostDlgWS(HWND hDlg, UINT message, WPARAM wParam, 
  432.                           LPARAM lParam)
  433. //-----------------------------------------------------------
  434. { switch (message) 
  435.    { case WM_INITDIALOG:       
  436.        hwndHostDlg = hDlg;
  437.        bHost = TRUE;  
  438.        if (!InitialiseWS())
  439.          { hwndHostDlg = NULL;           
  440.            bHost = FALSE;
  441.            EndDialog(hDlg, NULL);
  442.            return TRUE;
  443.          }       
  444.        SetDlgItemText(hDlg,IDS_WSMESS,szSockDesc);
  445.        if (! ServerStartWS())
  446.          { hwndHostDlg = NULL;           
  447.            bHost = bConnected = FALSE;
  448.            EndDialog(hDlg, NULL);
  449.            return TRUE;
  450.          }       
  451.        SetWindowText(hwndMain,"Winsock Chess (Waiting for Connection)");    
  452.        return TRUE;
  453.  
  454.      case WM_CONNECTED :       
  455. //       SetWindowText(hwndMain,"Winsock Chess (Connected)");  
  456.        hwndHostDlg = NULL;
  457.        if (wParam == 0)
  458.          bConnected = TRUE;
  459.        else
  460.          bConnected = bHost = FALSE;  
  461.        EnableMenuItems();  
  462.        EndDialog(hDlg, NULL);       
  463.        return TRUE;   
  464.  
  465.      case WM_COMMAND:     
  466.         switch (wParam)
  467.          { case IDC_ABORT : 
  468.               hwndHostDlg = NULL; 
  469.               bHost = FALSE;
  470.               DestroySocket(socketListen); 
  471.               DestroySocket(socketServer); 
  472.               WSACleanup();
  473.               EndDialog(hDlg, NULL);
  474.               return TRUE;   
  475.               
  476.          }
  477.     }
  478.  
  479.     return (FALSE); 
  480. }
  481.  
  482. BOOL CALLBACK ConnWSDlg(HWND hDlg, UINT message, WPARAM wParam, 
  483.                           LPARAM lParam)
  484. //--------------------------------------------------------------
  485. { char szIpAddr[50],szWinText[80];
  486.     
  487.   switch (message) 
  488.    { case WM_INITDIALOG:      
  489.        if (!InitialiseWS())
  490.          { EndDialog(hDlg, NULL);
  491.            return TRUE;
  492.          } 
  493.        GetHosts(hDlg);  
  494.        return TRUE;
  495.  
  496.      case WM_COMMAND:     
  497.         switch (wParam)
  498.          { case IDOK :             
  499.              SendDlgItemMessage(hDlg,IDC_COMBO1,WM_GETTEXT,(WPARAM)49,
  500.                          (LPARAM)((LPSTR)szIpAddr));
  501.              
  502.              SaveHosts(hDlg,szIpAddr); 
  503.              ShowWindow(GetDlgItem(hDlg,IDOK),SW_HIDE);
  504.              ShowWindow(GetDlgItem(hDlg,IDCANCEL),SW_HIDE);
  505.              SetWindowText(GetDlgItem(hDlg,IDS_WAIT),
  506.                            "Attempting connection - Please Wait");
  507.              if (ClientStartWS(szIpAddr))
  508.                { computer = white;
  509.                  opponent = black;
  510.                  User_Move = FALSE;
  511.                  bConnected = TRUE;
  512.                  flag.reverse = !flag.reverse;
  513.                  wsprintf(szWinText,"Winsock Chess (Connected to %s)",
  514.                           (LPSTR)szIpAddr);
  515.                  SetWindowText(hwndMain,szWinText);  
  516.                  UpdateDisplay(hwndMain,0,0,1,0);
  517.                  flag.force = false;
  518.                  Sdepth = 0;
  519.                  PostMessage ( hwndMain, MSG_COMPUTER_MOVE, NULL,NULL);  
  520.                }  
  521.              else
  522.                bConnected = FALSE;  
  523.              SetWindowText(GetDlgItem(hDlg,IDS_WAIT)," ");  
  524.              ShowWindow(GetDlgItem(hDlg,IDOK),SW_SHOW);  
  525.              ShowWindow(GetDlgItem(hDlg,IDCANCEL),SW_SHOW);  
  526.              EnableMenuItems();  
  527.              EndDialog(hDlg, NULL);
  528.              return TRUE;
  529.            
  530.            case IDCANCEL:
  531.              EndDialog(hDlg, NULL);
  532.              return TRUE;  
  533.          }     
  534.     }
  535.  
  536.     return (FALSE); 
  537. }
  538.  
  539.